/*
 * 文件名: CameraManager.java 版 权： Copyright Huawei Tech. Co. Ltd. All Rights
 * Reserved. 描 述: [该类的简要描述] 创建人: Administrator 创建时间:2012-8-16 修改人： 修改时间:
 * 修改内容：[修改内容]
 */
package com.collectplus.express.tools;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

/**
 * [照相机管理]<BR>
 * [功能详细描述]
 *
 * @author leung
 * @version [ShengZhiTravel, 2013-11-18]
 */
public class CameraManager {
    /**
     * 制定图片id的uri
     */
    public static final Uri MEDIA_IMAGE_URI_ID = Uri.parse("content://media/external/images/media/");
    private static CameraManager instance;

    public static CameraManager getInstance() {
        if (instance == null) {
            instance = new CameraManager();
        }
        return instance;
    }

    private CameraManager() {

    }

    /**
     * [照相机使用的请求码]<BR>
     * [功能详细描述]
     */
    public interface ICameraRequestCode {
        /**
         * 拍照基数
         */
        static final int BASE_CAMERA = 1000;
        /**
         * 裁剪
         */
        public static final int CAMERA_PHOTO_CUTTING = BASE_CAMERA + 1;
        /**
         * 选取照片(本地)
         */
        public static final int CAMERA_PHOTO_LOCAL = BASE_CAMERA + 2;
    }

    /**
     * [对选中的照片进行裁剪]<BR>
     * [功能详细描述]
     *
     * @param mActivity 上下文或当前对象
     * @param tempUri      裁剪的类型：1头像，2背景
     * @param uri       裁剪对象uri
     */
    public void cutting(Activity mActivity, String uri, String tempUri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        Uri mUri = Uri.parse(uri);
        intent.setDataAndType(mUri, "image/*");
        intent.putExtra("crop", "true");

        int aspectX = 0;
        int aspectY = 0;
        int outputX = 0;
        int outputY = 0;

        aspectX = 1;
        aspectY = 1;
        outputX = 600;
        outputY = 600;
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", aspectX);
        intent.putExtra("aspectY", aspectY);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);

        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(tempUri));// 返回大图必须用uri形式返回，
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());// 输出图片格式

        mActivity.startActivityForResult(intent, ICameraRequestCode.CAMERA_PHOTO_CUTTING);
    }

    /**
     * [对选中的照片进行裁剪]<BR>
     * [功能详细描述]
     *
     * @param mActivity 上下文或当前对象
     * @param uri       裁剪对象uri
     * @param x         x的比例
     * @param y         y的比例
     */
    public void cuttingWithXY(Activity mActivity, String uri, String tempUri, int x, int y) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        Uri mUri = Uri.parse(uri);
        intent.setDataAndType(mUri, "image/*");
        intent.putExtra("crop", "true");

        int aspectX = 0;
        int aspectY = 0;
        int outputX = 0;
        int outputY = 0;

        aspectX = x;
        aspectY = y;
        outputX = 600;
        outputY = 600;
        // aspectX aspectY 是宽高的比例
        if (aspectX != 0 && aspectY != 0) {
            intent.putExtra("aspectX", aspectX);
            intent.putExtra("aspectY", aspectY);
        }
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);

        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(tempUri));// 返回大图必须用uri形式返回，
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());// 输出图片格式

        mActivity.startActivityForResult(intent, ICameraRequestCode.CAMERA_PHOTO_CUTTING);
    }
    /**
     * [对选中的照片进行裁剪]<BR>
     * [功能详细描述]
     *
     * @param mActivity 上下文或当前对象
     * @param uri       裁剪对象uri
     * @param x         x的比例
     * @param y         y的比例
     */
    public void cuttingWithOutput(Activity mActivity, String uri, String tempUri, int x, int y, int outputX, int outputY) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        Uri mUri = Uri.parse(uri);
        intent.setDataAndType(mUri, "image/*");
        intent.putExtra("crop", "true");

        if (outputX == 0) {
            outputX = 600;
        }
        if (outputY == 0) {
            outputY = 600;
        }
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", x);
        intent.putExtra("aspectY", y);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", "true");
        intent.putExtra("noFaceDetection", "true"); // no face detection

        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(tempUri));// 返回大图必须用uri形式返回，
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());// 输出图片格式

        mActivity.startActivityForResult(intent, ICameraRequestCode.CAMERA_PHOTO_CUTTING);
    }
    /**
     * [选择本地图片]<BR>
     * [功能详细描述]
     *
     * @param mActivity 当前对象或上下文
     */
    public void localPhoto(Activity mActivity) {
        Log.i("leung", "localPhoto_调用系统照片");
        Intent mIntent = new Intent(Intent.ACTION_PICK);
        mIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        mActivity.startActivityForResult(mIntent, ICameraRequestCode.CAMERA_PHOTO_LOCAL);
    }

}
